//
//  ZLExamBeginCell.m
//  ZLExamination
//
//  Created by Zheng on 2017/8/18.
//  Copyright © 2017年 Zheng. All rights reserved.
//

#import "ZLExamBeginCell.h"
#import "ZLExamBeginTableQuestionTitleCell.h"
#import "ZLExamBeginTableQuestionListCell.h"

#import "ZLExamBeginModel.h"

@interface ZLExamBeginCell () <UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, copy) NSArray<NSString *> *questionTitles;
@end

@implementation ZLExamBeginCell

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        
        _questionTitles = @[@"A",@"B",@"C",@"D"];
        
        [self.contentView addSubview:self.myTableView];
        
        [_myTableView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.left.bottom.right.equalTo(self);
        }];
    }
    return self;
}

- (UITableView *)myTableView {
    if (!_myTableView) {
        _myTableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _myTableView.backgroundColor = [UIColor whiteColor];
        _myTableView.separatorStyle = UITableViewCellSelectionStyleNone;
        _myTableView.delegate = self;
        _myTableView.dataSource = self;
        _myTableView.bounces = NO;
        _myTableView.rowHeight = UITableViewAutomaticDimension;
        _myTableView.estimatedRowHeight = 100;
        [_myTableView registerClass:[ZLExamBeginTableQuestionTitleCell class] forCellReuseIdentifier:kZLExamBeginTableQuestionTitleCell_ID];
        [_myTableView registerClass:[ZLExamBeginTableQuestionListCell class] forCellReuseIdentifier:kZLExamBeginTableQuestionListCell_ID];

    }
    return _myTableView;
}

#pragma mark - tableview datasource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        return 1;
    } else {
        return 4;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *answerStr1 = [_model.op_answer stringByReplacingOccurrencesOfString:@"A"withString:@""];
    NSString *answerSt2 = [answerStr1 stringByReplacingOccurrencesOfString:@"B"withString:@""];
    NSString *answerSt3 = [answerSt2 stringByReplacingOccurrencesOfString:@"C"withString:@""];
    NSString *answerSt4 = [answerSt3 stringByReplacingOccurrencesOfString:@"D"withString:@""];
     NSString *answerSt5 = [answerSt4 stringByReplacingOccurrencesOfString:@":"withString:@""];
    NSArray *titles = [answerSt5 componentsSeparatedByString:@";"];

    if (indexPath.section == 0) {
        ZLExamBeginTableQuestionTitleCell *cell = [tableView dequeueReusableCellWithIdentifier:kZLExamBeginTableQuestionTitleCell_ID forIndexPath:indexPath];
        cell.questionLbl.text = _model.subject;
        return cell;
    } else {
        ZLExamBeginTableQuestionListCell *cell = [tableView dequeueReusableCellWithIdentifier:kZLExamBeginTableQuestionListCell_ID forIndexPath:indexPath];
        
        if (_model.isSingles) {
            //单选
            cell.isSelected = [_model.questionAnswer isEqualToString:_questionTitles[indexPath.row]] ? YES: NO;
        } else {
            //多选
            if (_model.answers.count == 0) {
                cell.multipleIsSelected = NO;
            } else {
                if ([_model.answers containsObject:_questionTitles[indexPath.row]]) {
                    cell.multipleIsSelected = YES;
                } else {
                    cell.multipleIsSelected = NO;
                }
            }
        }
        
        cell.optionsLbl.text = _questionTitles[indexPath.row];
        cell.questionLbl.text = titles[indexPath.row];
        
        return cell;
    }    
}

#pragma mark - tableview delegate
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.001;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0.001;
}

-  (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section != 0) {
        if (_answerCallBack) {
            _answerCallBack(_itemNumber, indexPath.row);
        }
    }
}


@end
